package com.sduept.nwld.dataserver.controller.config;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.message.entity.MessageType;
import com.sduept.message.manager.MessageReceiveConfigManager;
import com.sduept.message.manager.MessageTypeManager;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import com.sduept.utils.PingYinUtil;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.NodeUnselectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.List;

@Named
@ViewScoped
public class PushMessageTypeConfigController extends AbstractController {

	private static final long serialVersionUID = 6373540571518629647L;

	@Autowired
	private MessageTypeManager ma;
	@Autowired
	private MessageReceiveConfigManager relation;

	private List<MessageType> typeList = new ArrayList<MessageType>();
	private MessageType selected = null;
	private MessageType created;
	private TreeNode root;
	private TreeNode selectedNode;
	private boolean saveCanUse = false;

	@PostConstruct
	private void init() {
		initTreeNode();
	}

	/**
	 * 获取节点
	 */
	private void initTreeNode() {
		root = new LibTreeNode("treeRoot", null);
		TreeNode baseNode;
		List<MessageType> types = ma.findRoots();
		for (MessageType t : types) {
			baseNode = new LibTreeNode(t, root);
			baseNode.setExpanded(true);
			// 添加子节点
			List<MessageType> subTypes = ma.findTypeByPid(t.getId());
			getSubNode(baseNode, subTypes);
		}
	}

	public void onNodeSelect(NodeSelectEvent event) {
		selectedNode = event.getTreeNode();
		selected = (MessageType) selectedNode.getData();
	}

	public void onNodeUnSelect(NodeUnselectEvent event) {
		selected = null;
		selectedNode = null;
	}

	/**
	 * 获取每个节点的子节点
	 * 
	 * @return
	 */
	private void getSubNode(TreeNode node, List<MessageType> list) {
		for (MessageType bean : list) {
			TreeNode subNode = new DefaultTreeNode(bean, node);
			subNode.setExpanded(true);
		}
	}

	/**
	 * 新建类型前执行方法
	 */
	public void preCreate() {
		created = new MessageType();
	}

	/**
	 * 新建类型保存方法
	 */
	public void saveNew() {
		if (null != created) {
			if (created.getDescription() == null || "".equals(created.getDescription())
					|| created.getId() == null || "".equals(created.getId())) {
				addErrorMessage("请将信息填写完整！");
			} else {
				ma.create(created);
				addSuccessMessage("保存成功！");
			}
		}
		selected = null;
		selectedNode = null;
		initTreeNode();
	}

	/**
	 * 新建类型--检查id是否重复
	 */
	public void checkNameExit() {
		StringBuilder builder = new StringBuilder();
		String pid = "";
		MessageType t = null;
		if (selectedNode != null) {
			t = (MessageType) selectedNode.getData();
			pid = t.getPid();
		}
		if (StringUtils.isNoneEmpty(pid)) {
			builder.insert(0, pid + "_");
		}
		builder.append(created.getDescription());
		String code = PingYinUtil.getPingYinHeader(builder.toString());
		MessageType m = ma.findTypeById(code);
		if (m != null) {
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "错误", "该标识符已被占用，请修改类型名称！"));
			saveCanUse = false;
		} else {
			created.setId(code);
			created.setPid(t != null ? t.getPid() : null);
			saveCanUse = true;
		}
	}

	public void getNewSubId() {
		MessageType t = (MessageType) selectedNode.getData();
		String code = PingYinUtil.getPingYinHeader(t.getDescription() + "_" + created.getDescription());
		MessageType m = ma.findTypeById(code);
		if (m != null) {
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "错误", "该标识符已被占用！"));
		} else {
			created.setId(code);
			created.setPid(t.getId());
			saveCanUse = true;
		}
	}

	public void saveEdit() {
		if (selected != null) {
			ma.update(selected);
		}
		addSuccessMessage("保存成功！");
		selected = null;
		selectedNode = null;
		initTreeNode();
	}

	public void delete() {
		MessageType t = (MessageType) selectedNode.getData();
		List<MessageType> subNodes = new ArrayList<MessageType>();
		// 选中父节点的情况
		if (t.getPid() == null) {
			subNodes = ma.findTypeByPid(t.getId());
		}
		subNodes.add(t);
		ma.delete(subNodes);
		for (MessageType m : subNodes) {
			relation.delete(null, m.getId(), null);
		}
		addSuccessMessage("删除成功！");
		initTreeNode();
		selectedNode = null;
		selected = null;
	}

	public List<MessageType> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<MessageType> typeList) {
		this.typeList = typeList;
	}

	public MessageType getSelected() {
		return selected;
	}

	public void setSelected(MessageType selected) {
		this.selected = selected;
	}

	public MessageType getCreated() {
		return created;
	}

	public void setCreated(MessageType created) {
		this.created = created;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}

	public boolean isSaveCanUse() {
		return saveCanUse;
	}

	public void setSaveCanUse(boolean saveCanUse) {
		this.saveCanUse = saveCanUse;
	}

}
